<template>
  <div class="login-container">
    <div class="login-background">
      <div class="login-content">
        <div class="login-header">
          <div class="logo-section">
            <el-icon size="48" color="#2e7d32"><School /></el-icon>
            <h1>中山大学研究生院</h1>
            <p>校企对接平台</p>
          </div>
        </div>
        
        <el-card class="login-card" shadow="never">
          <template #header>
            <div class="card-header">
              <h2>{{ isLogin ? '用户登录' : '用户注册' }}</h2>
              <p>{{ isLogin ? '欢迎使用校企对接平台' : '创建您的账户' }}</p>
            </div>
          </template>
          
          <el-form 
            ref="formRef" 
            :model="form" 
            :rules="rules" 
            label-width="80px"
            size="large"
            @keyup.enter="handleSubmit"
          >
            <el-form-item label="用户名" prop="username">
              <el-input 
                v-model="form.username" 
                placeholder="请输入用户名"
                prefix-icon="User"
                clearable
              />
            </el-form-item>
            
            <el-form-item label="密码" prop="password">
              <el-input 
                v-model="form.password" 
                type="password" 
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                clearable
              />
            </el-form-item>
            
            <el-form-item v-if="!isLogin" label="确认密码" prop="confirmPassword">
              <el-input 
                v-model="form.confirmPassword" 
                type="password" 
                placeholder="请再次输入密码"
                prefix-icon="Lock"
                show-password
                clearable
              />
            </el-form-item>
            
            <el-form-item label="用户角色" prop="role">
              <el-select 
                v-model="form.role" 
                placeholder="请选择用户角色"
                style="width: 100%"
              >
                <el-option label="企业用户" value="enterprise">
                  <div class="role-option">
                    <el-icon color="#2e7d32"><OfficeBuilding /></el-icon>
                    <span>企业用户</span>
                    <small>发布课题、管理项目</small>
                  </div>
                </el-option>
                <el-option label="导师用户" value="supervisor">
                  <div class="role-option">
                    <el-icon color="#4caf50"><User /></el-icon>
                    <span>导师用户</span>
                    <small>申请课题、指导学生</small>
                  </div>
                </el-option>
                <el-option label="学生用户" value="student">
                  <div class="role-option">
                    <el-icon color="#66bb6a"><User /></el-icon>
                    <span>学生用户</span>
                    <small>申请课题、参与研究</small>
                  </div>
                </el-option>
                <el-option label="管理员" value="admin">
                  <div class="role-option">
                    <el-icon color="#ff9800"><Setting /></el-icon>
                    <span>管理员</span>
                    <small>系统管理、数据统计</small>
                  </div>
                </el-option>
              </el-select>
            </el-form-item>
            
            <el-form-item v-if="!isLogin" label="真实姓名" prop="realName">
              <el-input 
                v-model="form.realName" 
                placeholder="请输入真实姓名"
                prefix-icon="User"
                clearable
              />
            </el-form-item>
            
            <el-form-item v-if="!isLogin" label="邮箱" prop="email">
              <el-input 
                v-model="form.email" 
                placeholder="请输入邮箱地址"
                prefix-icon="Message"
                clearable
              />
            </el-form-item>
            
            <el-form-item v-if="!isLogin" label="手机号" prop="phone">
              <el-input 
                v-model="form.phone" 
                placeholder="请输入手机号码"
                prefix-icon="Phone"
                clearable
              />
            </el-form-item>
            
            <el-form-item v-if="isLogin">
              <div class="login-options">
                <el-checkbox v-model="form.rememberMe">记住密码</el-checkbox>
                <el-button type="text" @click="forgotPassword">忘记密码？</el-button>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                :loading="loading"
                @click="handleSubmit"
                style="width: 100%"
              >
                {{ isLogin ? '登录' : '注册' }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="form-footer">
            <span>{{ isLogin ? '还没有账户？' : '已有账户？' }}</span>
            <el-button type="text" @click="toggleMode">
              {{ isLogin ? '立即注册' : '立即登录' }}
            </el-button>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { School, User, Lock, Message, Phone, OfficeBuilding, Setting } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const isLogin = ref(true)
const loading = ref(false)
const formRef = ref()

const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  role: '',
  realName: '',
  email: '',
  phone: '',
  rememberMe: false
})

const rules = computed(() => {
  const baseRules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
    ],
    role: [
      { required: true, message: '请选择用户角色', trigger: 'change' }
    ]
  }
  
  if (!isLogin.value) {
    return {
      ...baseRules,
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
          validator: (rule: any, value: string, callback: Function) => {
            if (value !== form.password) {
              callback(new Error('两次输入密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ],
      realName: [
        { required: true, message: '请输入真实姓名', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入手机号码', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
      ]
    }
  }
  
  return baseRules
})

const toggleMode = () => {
  isLogin.value = !isLogin.value
  formRef.value?.resetFields()
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    if (isLogin.value) {
      // 登录逻辑
      const result = await userStore.login(form.username, form.password, form.role)
      if (result.success) {
        ElMessage.success('登录成功！')
        
        // 根据角色跳转到不同页面
        const roleRoutes: Record<string, string> = {
          enterprise: '/publish',
          supervisor: '/projects',
          student: '/projects',
          admin: '/admin'
        }
        
        const targetRoute = roleRoutes[form.role] || '/'
        router.push(targetRoute)
      } else {
        ElMessage.error(result.message || '登录失败')
      }
    } else {
      // 注册逻辑
      ElMessage.success('注册成功！请登录')
      isLogin.value = true
      formRef.value.resetFields()
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  } finally {
    loading.value = false
  }
}

const forgotPassword = () => {
  ElMessage.info('请联系管理员重置密码')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f8f9fa 0%, #e8f5e8 100%);
}

.login-background {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-content {
  width: 100%;
  max-width: 1000px;
  display: flex;
  align-items: center;
  gap: 60px;
}

.login-header {
  flex: 1;
  text-align: center;
  color: var(--primary-color);
}

.logo-section h1 {
  font-size: 32px;
  font-weight: 700;
  margin: 20px 0 10px;
  color: var(--primary-color);
}

.logo-section p {
  font-size: 18px;
  color: var(--text-secondary);
  margin: 0;
}

.login-card {
  flex: 1;
  max-width: 480px;
  border-radius: 16px;
  border: 1px solid var(--border-color);
  box-shadow: 0 8px 32px rgba(46, 125, 50, 0.1);
}

.card-header {
  text-align: center;
}

.card-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: var(--primary-color);
  margin: 0 0 8px;
}

.card-header p {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

.role-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
}

.role-option span {
  font-weight: 500;
}

.role-option small {
  color: var(--text-secondary);
  font-size: 12px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border-color);
  color: var(--text-secondary);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input,
.el-select {
  border-radius: 8px;
}

.el-input:focus,
.el-select:focus {
  border-color: var(--primary-color);
}

.el-button {
  border-radius: 8px;
  font-weight: 500;
}

.el-button--primary {
  height: 48px;
  font-size: 16px;
}

@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
    gap: 30px;
  }
  
  .login-header {
    order: 2;
  }
  
  .login-card {
    order: 1;
    max-width: 100%;
  }
}
</style>
